﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;

namespace Huirui.Cavan.Presentation.Controller.StockModule
{
    public class WareHouseController : EntityController<WareHouse, string, WareHouseSearch, IWareHouseViewModel, IWareHouseAppService>, IWareHouseController
    {
        public override string ControllerName
        {
            get { return ControllerConst.WAREHOUSE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public WareHouseController()
        {
            this.Model = ModelFactory.CreateModel<IWareHouseViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "warehouse_query")]
        public ActionResult AddWareHouse()
        {
            return View("~/Views/Stock/v1/addWareHouse.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "warehouse_edit")]
        public JsonResult AddWareHouse(WareHouseViewModel model)
        {
            BoolResult jsonResult = null;
            BoolGenericResult<WareHouse> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "warehouse_query")]
        public JsonResult GetWareHouses(WareHouseViewModel model)
        {
            WareHouseJsonViewModel json = new WareHouseJsonViewModel();
            json.rows = new List<WareHouseJson>();

            IList<WareHouse> wareHouses = this.AppService.GetBySearch(SearchFactory.CreateWareHouseSearch()).ToList();
            foreach (WareHouse wh in wareHouses)
            {
                WareHouseJson whj = new WareHouseJson();
                whj.Id = wh.Id;
                whj.Address = wh.Address;

                json.rows.Add(whj);
            }
            json.total = wareHouses.Count;

            return Json(json, "application/json", JsonRequestBehavior.AllowGet);
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "warehouse_query")]
        public ActionResult GetWareHouses()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/Stock/v1/partial/selectWareHousePartial.cshtml", this.Model);
        }
    }
}
